package com.maochd.cloud.excel.utils;

import com.alibaba.excel.metadata.data.DataFormatData;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import lombok.experimental.UtilityClass;
import org.apache.poi.ss.usermodel.*;

/**
 * EasyExcel 样式
 */
@UtilityClass
public class StyleUtils {

    /**
     * 通用样式配置
     */
    public WriteHandler globalWriteHandler() {
        // 标题样式策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 自动换行
        headWriteCellStyle.setWrapped(false);
        // 背景灰色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
        // 标题字体策略
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 11);
        headWriteFont.setBold(true);
        headWriteFont.setColor(IndexedColors.WHITE.getIndex());
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 内容样式策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 自动换行
        contentWriteCellStyle.setWrapped(false);
        // 垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND
        // 不然无法显示背景颜色，头默认了 FillPatternType所以可以不指定
        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 背景白色
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 11);
        contentWriteFont.setColor(IndexedColors.BLACK.getIndex());
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 设置内容边框样式
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        // 设置单元格格式为文本
        DataFormatData dataFormatData = new DataFormatData();
        String format = BuiltinFormats.getBuiltinFormat(49);
        dataFormatData.setFormat(format);
        contentWriteCellStyle.setDataFormatData(dataFormatData);
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}
